home *** CD-ROM | disk | FTP | other *** search
- // BEGIN FLOCK GPL
- //
- // Copyright Flock Inc. 2005-2007
- // http://flock.com
- //
- // This file may be used under the terms of of the
- // GNU General Public License Version 2 or later (the "GPL"),
- // http://www.gnu.org/licenses/gpl.html
- //
- // Software distributed under the License is distributed on an "AS IS" basis,
- // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- // for the specific language governing rights and limitations under the
- // License.
- //
- // END FLOCK GPL
- //
-
- const FLOCK_SHELF_CID = Components.ID('{ee266aa8-bc64-4e6a-9a20-1143b0444fbd}');
-
- const nsISupports = Components.interfaces.nsISupports;
- const nsIClassInfo = Components.interfaces.nsIClassInfo;
- const flockIShelfService = Components.interfaces.flockIShelfService;
-
- const FLOCK_SHELF_CONTRACTID = '@mozilla.org/rdf/datasource;1?name=flock-shelf';
-
- const FLOCK_NS = 'http://flock.com/rdf#';
- const FLOCK_SHELF_ROOT = 'urn:flock:shelfroot';
-
- //Interfaces
- const nsIFactory = Components.interfaces.nsIFactory;
-
-
- function flockShelfService() {
- this._coop = Components.classes['@flock.com/singleton;1'].getService(Components.interfaces.flockISingleton).getSingleton('chrome://browser/content/flock/common/load-faves-coop.js').wrappedJSObject;
-
- this.shelfRoot = this._coop.get(FLOCK_SHELF_ROOT);
- if (!this.shelfRoot) {
- this.shelfRoot = new this._coop.Folder(FLOCK_SHELF_ROOT);
- this._coop.favorites_root.children.add(this.shelfRoot);
- }
- }
-
-
- // Shelf Interface Implementation
- flockShelfService.prototype.elementsCount = function () {
- var cnt=0;
-
- var e = this.shelfRoot.children.enumerate();
- while (e.hasMoreElements()) {
- e.getNext();
- cnt++;
- }
-
- return cnt;
- }
-
- flockShelfService.prototype.insert = function (aTitle, aContent, aUrl, aType, aPos, aParent) {
- var date = new Date();
- var id = FLOCK_SHELF_ROOT + ':obj_' + Date.now() + ":" + Math.round(100*Math.random());
-
- var parentNode = aParent?this._coop.get(aParent):this.shelfRoot;
- var item = new this._coop.WebSnippet(id,
- {
- content: aContent,
- name: aTitle,
- creationDate: date,
- URL: aUrl,
- snippetType: aType
- });
- item.save();
-
- if (aPos < 0)
- parentNode.children.add(item);
- else
- parentNode.children.insertAt(item, aPos);
-
- this._refreshCount(parentNode);
- return id;
- }
-
-
- flockShelfService.prototype.createFolder = function (aName) {
- var id = FLOCK_SHELF_ROOT + ':folder:' + aName;
- var folder = new this._coop.Folder(id, {name: aName});
-
- this.shelfRoot.children.add(folder);
-
- return id;
- }
-
- flockShelfService.prototype.moveTo = function (aId, aFolder) {
- var folderId = FLOCK_SHELF_ROOT + ':folder:' + aFolder;
- var item = this._coop.get(aId);
- if (!item) {
- debug("*** ERROR: Can't find "+aId+"\n");
- return;
- }
- var folder = this._coop.get(folderId);
- if (!folder) {
- this.createFolder(aFolder);
- folder = this._coop.get(folderId);
- }
-
- if (folder.children.indexOf(item) > 0)
- return; // Already in the folder
-
- var parent = item.getParent();
- parent.children.remove(item);
- folder.children.add(item);
-
- this._refreshCount(parent);
- this._refreshCount(folder);
- }
-
- flockShelfService.prototype.changeAttribute = function (aUrn, aAttribute, aValue){
- var snippet = this._coop.get(aUrn);
- snippet[aAttribute] = aValue;
- snippet.save();
- }
-
- flockShelfService.prototype.getPositionOf = function (aUrn){
- var snippet = this._coop.get(aUrn);
- var parent = snippet.getParent()
- return parent.children.indexOf(snippet);
- }
-
- flockShelfService.prototype.getTargetOf = function (aUrn, aTarget) {
- var snippet = this._coop.get(aUrn);
- if (snippet)
- return snippet[aTarget];
- else
- return null;
- }
-
- flockShelfService.prototype.remove = function (aUrn) {
- var snippet = this._coop.get(aUrn);
- var parent = snippet.getParent();
- parent.children.remove(snippet);
- this._refreshCount(parent);
-
- snippet.destroy();
- }
-
- flockShelfService.prototype._refreshCount = function (aFolder) {
- if (!aFolder.children)
- return;
-
- var count = 0;
- var _enum = aFolder.children.enumerate();
- while (_enum.hasMoreElements()) {
- _enum.getNext();
- ++count;
- }
- aFolder.count = count;
- }
-
-
- flockShelfService.prototype.clear =
- function () {
- var e = this.shelfRoot.children.enumerate();
- var elts = [];
- while (e.hasMoreElements())
- elts.push(e.getNext());
- while (elts.length > 0){
- var elt = elts.pop();
- this.shelfRoot.children.remove(elt);
- elt.destroy();
- }
- }
-
-
- function loadSubScript(spec)
- {
- var loader = Components.classes['@mozilla.org/moz/jssubscript-loader;1']
- .getService(Components.interfaces.mozIJSSubScriptLoader);
- var context = {};
- loader.loadSubScript(spec, context);
- return context;
- }
-
- var Coop = loadSubScript('chrome://browser/content/flock/common/coop.js').Coop;
-
- /////////////////////////////////////////////////////////////////////
- // XPCOM Registration
- /////////////////////////////////////////////////////////////////////
-
- flockShelfService.prototype.flags = nsIClassInfo.Singleton;
- flockShelfService.prototype.classDescription = "Flock Shelf Service";
- flockShelfService.prototype.getInterfaces = function (count) {
- var interfaceList = [flockIShelfService, nsIClassInfo];
- count.value = interfaceList.length;
- return interfaceList;
- }
- flockShelfService.prototype.getHelperForLanguage = function (count) {return null;}
-
- // the nsISupports implementation
- flockShelfService.prototype.QueryInterface =
- function (iid) {
- if (!iid.equals(flockIShelfService) &&
- !iid.equals(nsIClassInfo) &&
- !iid.equals(nsISupports))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
-
- // Module implementation/*{{{*/
- var ShelfModule = new Object();
-
- ShelfModule.registerSelf =
- function (compMgr, fileSpec, location, type) {
- compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
-
- compMgr.registerFactoryLocation(FLOCK_SHELF_CID,
- "Flock Shelf JS Component",
- FLOCK_SHELF_CONTRACTID,
- fileSpec,
- location,
- type);
- }
-
- ShelfModule.getClassObject =
- function (compMgr, cid, iid) {
- if (!cid.equals(FLOCK_SHELF_CID))
- throw Components.results.NS_ERROR_NO_INTERFACE;
-
- if (!iid.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- return ShelfServiceFactory;
- }
-
- ShelfModule.canUnload =
- function(compMgr) {
- //gPhotoService.dataSource.Flush();
- return true;
- }
-
- /* factory object */
- var ShelfServiceFactory = new Object();
-
- ShelfServiceFactory.createInstance =
- function (outer, iid) {
- if (outer != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
-
- return (new flockShelfService()).QueryInterface(iid);
- }
-
- /* entrypoint */
- function NSGetModule(compMgr, fileSpec) {
- return ShelfModule;
- }
-
-